package com.tyut.musicdreamer.service.user.controller;

import com.tyut.musicdreamer.service.user.model.vo.UserVO;
import com.tyut.musicdreamer.service.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户控制器 - 处理用户相关操作
 */
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;
    private static final String USER_ID_HEADER = "X-User-Id";

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/info")
    public UserVO getUserInfo(@RequestHeader(USER_ID_HEADER) Long userId) {
        log.debug("获取用户信息, 用户ID: {}", userId);
        return userService.getUserInfo(userId);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/info")
    public UserVO updateUserInfo(@RequestHeader(USER_ID_HEADER) Long userId,
                                 @RequestParam String username,
                                 @RequestParam String email) {
        log.debug("更新用户信息, 用户ID: {}, 用户名: {}, 邮箱: {}", userId, username, email);
        return userService.updateUserInfo(userId, username, email);
    }

    /**
     * 更新用户头像
     */
    @PostMapping("/avatar")
    public String updateAvatar(@RequestHeader(USER_ID_HEADER) Long userId,
                               @RequestParam("file") MultipartFile file) {
        log.debug("更新用户头像, 用户ID: {}, 文件大小: {}", userId, file.getSize());
        return userService.updateAvatar(userId, file);
    }

    /**
     * 修改密码
     */
    @PostMapping("/password")
    public boolean updatePassword(@RequestHeader(USER_ID_HEADER) Long userId,
                                  @RequestParam String oldPassword,
                                  @RequestParam String newPassword) {
        log.debug("修改密码, 用户ID: {}", userId);
        return userService.updatePassword(userId, oldPassword, newPassword);
    }

    /**
     * 获取指定用户信息
     */
    @GetMapping("/{userId}")
    public UserVO getUserById(@PathVariable Long userId) {
        log.debug("获取指定用户信息, 用户ID: {}", userId);
        return userService.getUserInfo(userId);
    }
    
    /**
     * 获取用户名
     */
    @GetMapping("/name/{userId}")
    public String getUserName(@PathVariable Long userId) {
        log.debug("获取用户名, 用户ID: {}", userId);
        UserVO userVO = userService.getUserInfo(userId);
        return userVO != null ? userVO.getUsername() : "用户" + userId;
    }
    
    /**
     * 获取用户头像
     */
    @GetMapping("/avatar/{userId}")
    public String getUserAvatar(@PathVariable Long userId) {
        log.debug("获取用户头像, 用户ID: {}", userId);
        UserVO userVO = userService.getUserInfo(userId);
        return userVO != null ? userVO.getAvatar() : null;
    }
} 